#!/bin/sh

# echo "%" "$@"

JAVA_ARGS="-server -Xss64M -Xms2G -Xmx8G -XX:NewSize=512M"
LVL="COMPET"
SOLS=-1
TIME_LIMIT=-1
CHOCO_JAR=/Users/kyzrsoze/Sources/CHOCO/continuous-branch/parsers/target/choco-parsers-4.10.15-SNAPSHOT-light.jar
usage="\

Usage: fzn_choco.sh [<options>] [<file>]

    Parse and solve <file> using Choco.

OPTIONS:

    -h, --help
        Display this message.

    -a
        This causes the solver to search for, and output all solutions.
        When this option is not given the solver should search for, and output the first solution or the best known one.

    -f
        When invoked with this option the solver ignores any specified search strategy.

    -v
        When invoked with this option verbose solving is activated.

    -s
        When invoked with this option the solver outputs statistics for solving

    -p
        When invoked with this option the solver is free to use multiple threads and/or cores during search.
        The argument n specifies the number of cores that are available.  (The default is $NB_NODES.)

    -t <n>
        Limit the resolution time of each problem instance to n ms.  (The default is $TIME_LIMIT.)

    -jar <j>
        Override the jar file.  (The default is $CHOCO_JAR.)

    --cp-profiler id,port
        Enable the cp-profiler with the given id and port.

    --jargs <args>
		    Override default java argument (The default is $JAVA_ARGS.)
		
EXAMPLES:
	
	Basic command to solve a fzn model with choco:
	$> sh fzn_exec -jar /path/to/choco-parsers-with-dep.jar ./alpha.fzn

	Additionnal arguments:
	$> sh fzn_exec --jargs \"-Xmx128m\" -tl 100 -jar /path/to/choco-parsers-with-dep.jar ./alpha.fzn

"

if test $# -eq 0
then
    echo "%% No flatzinc file found"
    exit 1
fi

ARGS=""

while test $# -gt 0
do
    case "$1" in

        -h|--help)
            echo "$usage"
            exit 0
        ;;

        -a)
            ARGS="$ARGS -a"
        ;;

        -f)
            ARGS="$ARGS -f"
        ;;

        -p)
            ARGS="$ARGS -p $2"
            shift
        ;;

        -t)
            TIME_LIMIT="$2"
            shift
        ;;

        -v)
            LVL="INFO"
        ;;

       -s)
            ARGS="$ARGS -stasol"
        ;;

        -n)
            SOLS="$2"
            shift
        ;;

        -r)
            ARGS="$ARGS -seed $2"
            shift
        ;;

        -jar)
            CHOCO_JAR="$2"
            shift
        ;;

        --jargs)
              JAVA_ARGS="$2"
              shift
          ;;

        --cp-profiler)
              ARGS="$ARGS --cp-profiler $2"
              shift
          ;;

        -*)
            echo "$0: unknown option \`$1'" 1>&2
            echo "$usage" 1>&2
            exit 2
        ;;

        *)
           break
        ;;

    esac
    shift
done

FILE="$1"

ARGS="$ARGS -limit=[$TIME_LIMIT,${SOLS}sols] -lvl ${LVL}"

CMD="java ${JAVA_ARGS} -cp .:${CHOCO_JAR} org.chocosolver.parser.flatzinc.ChocoFZN \"${FILE}\" ${ARGS}"

if test "${LVL}" = "INFO"
then
  echo "% $CMD"
fi
eval "${CMD}"

